<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2019/3/4
 * Time: 9:05
 */

namespace app\admin\controller;


use app\admin\model\Account;
use app\admin\model\Popularize as PopularizeModel;
use app\admin\service\RegisteredService;
use app\admin\validate\getPhoneDataValidate;
use app\admin\validate\MoneyPhoneValidate;
use app\api\validate\IDMustBePositiveInt;
use app\common\controller\Info;
use app\common\controller\RegisteredPhone;
use app\common\service\getPhoneInfoService;
use app\admin\model\Registered as RegisteredModel;
use think\facade\Request;

class Registered extends BaseController
{
    public function index()
    {
        $model = new Account();
        $name = $model->field('name')->select();
        $this->assign('name',$name);
        return $this->fetch('registered/index');
    }

    /**
     * 根据手机号码查询出贷款详情
     * @param $phone 手机号码
     * @param $type
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    public function getMoneyInfo($phone, $type, $logo)
    {
        (new MoneyPhoneValidate()) ->goCheck();
        $common = new Info();
        $result = $common->getPhoneInfo($phone, $type, $logo);
        return $result;
    }

    /**
     * 根据传过来的json字符串来更新对应每条里面手机号码的贷款状态
     */
    public function getAllPhoneInfo()
    {
        $json = Request::post('jsons');
        $service = new getPhoneInfoService();
        $result = $service->updatePhoneInfo($json);
        return json($result);
    }

    /**
     * 删除一条注册记录
     */
    public function delOneRegistered($id)
    {
        (new IDMustBePositiveInt()) -> goCheck();
        $result = RegisteredModel::destroy($id);
        if(!$result){
            throw new MysqlErrorException([
                'msg' => '服务器出错',
                'code' => 500
            ]);
        }
        return json([
            'code' => 200,
            'msg' => 'success'
        ]);
    }
    # 根据id删除对应的记录
    public function delCheckListApi()
    {
        $ids = Request::post('ids');
        $ids_arr = json_decode($ids, true);
        $model = new RegisteredModel();
        $model->delCheck($ids_arr);
        return json(['code' => 200]);
    }

    # 渲染没有借款的页面
    public function noMoney()
    {
        $model = new Account();
        $name = $model->field('name')->select();
        $this->assign('name',$name);
        return $this->fetch('registered/noMoney');
    }

    # 获取没有借款的所有记录
    public function noMoneyApi($page, $limit)
    {
        $service = new RegisteredService();
        $result = $service->getAllNoMeney($page, $limit);
        return json($result);
    }

    # 搜索没有借款的记录
    public function findAllNoMoneyApi()
    {
        $param = Request::get();
        $service = new RegisteredService();
        $result = $service->findAll($param);
        return json($result);
    }
}